package com.om.abom.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.abom.entity.AppServerEntity;
import com.om.abom.mapper.AppServerMapper;
import com.om.abom.service.AppServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import static com.om.abom.utils.JschExecutor.execMultiCmd;
import static com.om.abom.utils.ProcessManagement.processStart;
import static com.om.abom.utils.ProcessManagement.processStop;


@Slf4j
@Service("appServerService")
public class AppServerServiceImpl extends ServiceImpl<AppServerMapper, AppServerEntity> implements AppServerService
{
    @Override
    public String start(long id)
    {
        AppServerEntity appServer = getById(id);
        String hostIp = appServer.getHostIp();
        log.warn("start host {}", hostIp);
        return execMultiCmd(hostIp, executor -> {
            try {
                return processStart(executor);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    @Override
    public String stop(long id)
    {
        AppServerEntity appServer = getById(id);
        String hostIp = appServer.getHostIp();
        log.warn("stop host {}", hostIp);
        return execMultiCmd(hostIp, executor -> {
            try {
                return processStop(executor);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
}